<?php
/**
 * 《家庭财务》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../finance/Borrow.class.php');

class BorrowSun extends Borrow {
	public $tbl = Constant::tbl_cw_sun;
	
	public $type = array(
		'-' => '请选择',
		'1' => '储备金',
		'2' => '临时',		
		'3' => '私人',
		'4' => '预付货款',
	
		'5' => '信用卡还款',
		'6' => '工资',
		
		'9' => '其它',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
function txt($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "txt";
		
		$insql = $this->_batch_insql($request);
		if (empty($insql)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));

			$this->findPost($controller, $request);
			return false;
		}
		
		$my_bank = $this->_my_bank($_SESSION['id'], 5);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['payment'] != '2') {
				continue;
			}
			
			$row['bank'] = $this->_bank("1", $row['emp'], $row['payment_x']);
			if (!is_array($row['bank'])) {
				continue;
			}
			
			$row['transmit'] = $this->_transmit_type($row['bank'], $my_bank['zone'], $my_bank['city']);
			if (empty($row['transmit'])) {
				continue;
			}
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('bank', $my_bank);
		
		$content = $controller->sunxy($request, 'finance/borrow/list');
		$content = iconv("UTF-8", "GBK", $content);
		
		$filename = sprintf("BATCH-%s.PAY", date("YmdHis"));
		header('Content-Disposition:text/vcf; filename=' . $filename);
		
		$controller->assign_by_ref('content', $content);
		$controller->display($request, 'gbk');
		return true;
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'finance/borrow/sun/find');
		return true;
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/borrow/sun/list');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$this->_files_clean($id);
	
		$row = $this->_view($id);
	
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/borrow/sun/upload');
		return true;
	}
	
	function batch($controller, $request) {
		$this->pretreat($controller);
	
		$controller->assign('t1', strtotime('10 day'));
	
		$controller->display($request, 'finance/borrow/sun/batch_add');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function batchPost($controller, $request) {
		$this->pretreat($controller);
	
		$type = $request['type'];
		$name2 = addslashes($request['name']);
		$content = addslashes($request['content']);
	
		$begin = $request['begin'];
		$end = $request['end'];
		
		$data = json_decode($request['item'], true);
	
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, type, name, content, begin, end, amount, currency, payment, payment_x, oper, createtime, ";
		$fmt .= "modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		
		while (list($k, $v) = each($data)) {
				
			if (empty($v['emp']) || empty($v['amount'])) {
				continue;
			}	
				
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
				
			$amount = $v['amount'];
			$currency = $v['currency'];
			
			$payment = '2';
			$payment_x = $this->_payment_x($emp, '2');
				
			$name = addslashes($v['name']);
			if (empty($name)) {
				$name = $name2;
			}
			$content = addslashes($v['content']);
				
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $type, $name, $content, $begin, $end, $amount, $currency, $payment, $payment_x, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function update($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$t = array();
			$t['id'] = $row['id'];
			
			$t['begin'] = $row['begin'];
			$t['eng'] = $row['end'];
				
			$t['emp'] = sprintf("%s:%s", $row['emp'], $row['emp_t']);
				
			$t['currency'] = $row['currency'];
			$t['amount'] = $row['amount'];
				
			$t['type'] = $row['type'];
			$t['name'] = $row['name'];
			$t['content'] = $row['content'];
			
			$t['status'] = $row['status'];
				
			$data[] = $t;
	
			// $data[] = $row;
				
			$controller->assign_by_ref('result', $t);
		}
	
		$controller->assign_by_ref('data', json_encode($data));
	
		$controller->display($request, 'finance/borrow/sun/batch_edit');
		return true;
	}
	
	function updatePost($controller, $request) {
		$this->pretreat($controller);
	
		$type = $request['type'];
		$name2 = addslashes($request['name']);
		$content = addslashes($request['content']);
	
		$begin = $request['begin'];
		$end = $request['end'];
		
		$data = json_decode($request['item'], true);
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', type = '%s', begin = '%s', end = '%s', currency = '%s'";
		$fmt .= ", amount = '%s', name = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
	
		while (list($k, $v) = each($data)) {
				
			if (empty($v['emp']) || empty($v['amount'])) {
				continue;
			}
			
			$emp = $this->_input($v['emp']);
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
			
			$currency = $v['currency'];
			$amount = $v['amount'];
				
			$name = addslashes($v['name']);
			if (empty($name)) {
				$name = $name2;
			}
			
			$content = addslashes($v['content']);
				
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $type, $begin, $end, $currency, $amount, $name, $content, $v['id']);
			debug($sql);
			$db->query($sql);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_copy($controller, $request) {
		$this->pretreat($controller);
	
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s ORDER BY id";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$t = array();
			$t['id'] = $row['id'];
				
			$t['begin'] = $row['begin'];
			$t['eng'] = $row['end'];
	
			$t['emp'] = sprintf("%s:%s", $row['emp'], $row['emp_t']);
	
			$t['currency'] = $row['currency'];
			$t['amount'] = $row['amount'];
	
			$t['type'] = $row['type'];
			$t['name'] = $row['name'];
			$t['content'] = $row['content'];
				
			$t['status'] = $row['status'];
	
			$data[] = $t;
	
			// $data[] = $row;
	
			$controller->assign_by_ref('result', $t);
		}
	
		$controller->assign_by_ref('data', json_encode($data));
	
		$controller->display($request, 'finance/borrow/sun/batch_copy');
		return true;
	}
}
?>